$(function () {

    function inde() {
        $.ajax({
            url: bignews.category_list,
            headers: { Authorization: localStorage.getItem('name') },
            success: function (src) {
                console.log(src);
                let html = template('muotemp', src)
                $('tbody').html(html)
            }
        })

    }
    inde()

    let id = null


    $('#xinzengfenlei').on('click', function () {
        $('#myModalLabel').text('新增分类')
        $('.btnadd').text('新增')
        $('#exampleInputEmail1').val('')
        $('#exampleInputPassword1').val('')
    })
    $('tbody').on('click', '.btnaddd', function () {
        $('#myModal').modal('show')
        $('#myModalLabel').text('编辑分类')
        $('.btnadd').text('编辑')

        $('#exampleInputEmail1').val($(this).data().name)
        $('#exampleInputPassword1').val($(this).data().slug)
        id = $(this).data().id
    })

    $('.btnadd').on('click', function () {
        if ($('.btnadd').text() === '新增') {

            let name = $('#exampleInputEmail1').val()
            let slug = $('#exampleInputPassword1').val()
            $.ajax({
                url: bignews.category_add,
                headers: { Authorization: localStorage.getItem('name') },
                type: 'post',
                data: { name, slug },
                success: function (src) {
                    inde()
                }
            })



        } else {
            let name = $('#exampleInputEmail1').val()
            let slug = $('#exampleInputPassword1').val()
            $.ajax({
                url: bignews.category_edit,
                type: 'post',
                headers: { Authorization: localStorage.getItem('name') },
                data: { id, name, slug },
                success: function (src) {
                    inde()
                }
            })
        }
    })
    $('tbody').on('click', '.btn-del', function () {
        let id = $(this).data().id
        $.ajax({
            url: bignews.category_delete,
            type: 'post',
            data: { id },
            headers: { Authorization: localStorage.getItem('name') },
            success: function (src) {
                console.log(src);
                inde()
            }
        })
    })
})